package czk.concrent.moreLock;

import java.util.concurrent.TimeUnit;

/**
 * @Author:ChenZhangKun
 * @Date: 2020/5/31 15:05
 */
// 死锁的演示
public class DeadLock {
    public static void main(String[] args) {
        // 第一把锁
        Object obj1=new Object();
        // 第二把把锁
        Object obj2=new Object();
        // 线程一
        new Thread(()->{
            synchronized (obj1){
                try {
                    System.out.println("线程一获取第一把锁");
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj2){
                    System.out.println("线程一获取第二把锁");
                }
            }
        },"t1").start();
        new Thread(()->{
            synchronized (obj2){
                System.out.println("线程二获取第一把锁");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (obj1){
                    System.out.println("线程二获取第二把锁");
                }
            }
        },"t2").start();
    }
}
